package com.example.xutildemoproject.fragment;

import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.xutildemoproject.R;
import com.luck.picture.lib.PictureSelectionModel;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageSimpleListFragment;
import com.xuexiang.xutil.app.IntentUtils;
import com.xuexiang.xutil.app.SocialShareUtils;
import com.xuexiang.xutil.common.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;
import static com.xuexiang.xutil.app.IntentUtils.DocumentType.ANY;
import static com.xuexiang.xutil.app.IntentUtils.DocumentType.IMAGE;
import static com.xuexiang.xutil.app.IntentUtils.DocumentType.VIDEO;

@Page(name = "社会化分享")
public class SocialShareFragment extends XPageSimpleListFragment {
    public static final int REQUEST_CODE_SHARE_PICTURE = 99;
    public static final int REQUEST_CODE_MULTIPLE_PICTURE = 100;
    public static final int REQUEST_CODE_SHARE_FILE = 102;
    public static final int REQUEST_CODE_SHARE_VIDEO = 103;
    private SocialShareUtils.ShareType mShareType = SocialShareUtils.ShareType.DEFAULT;
    @Override
    protected List<String> initSimpleData(List<String> lists) {
        lists.add("分享单图片");
        lists.add("分享单图片到微信朋友圈");
        lists.add("分享单图片到微信联系人");
        lists.add("分享多图片");
        lists.add("分享多图片到微信朋友圈");
        lists.add("分享多图片到微信联系人");
        lists.add("分享视频");
        lists.add("分享视频到微信联系人");
        lists.add("分享文件");
        lists.add("分享文件到微信联系人");
        return lists;
    }

    @Override
    protected void onItemClick(int position) {
        switch (position) {
            case 0:
                share(SocialShareUtils.ShareType.DEFAULT,IMAGE);
                break;
            case 1:
                share(SocialShareUtils.ShareType.WE_CHAT_CIRCLE,IMAGE);
                break;
            case 2:
                share(SocialShareUtils.ShareType.WE_CHAT_CONTACTS,IMAGE);
                break;
            case 3:
                mShareType = SocialShareUtils.ShareType.DEFAULT;
                getPictureSelector(this)
                        .forResult(REQUEST_CODE_MULTIPLE_PICTURE);
                break;
            case 4:
                mShareType = SocialShareUtils.ShareType.WE_CHAT_CIRCLE;
                getPictureSelector(this)
                        .forResult(REQUEST_CODE_MULTIPLE_PICTURE);
                break;
            case 5:
                mShareType = SocialShareUtils.ShareType.WE_CHAT_CONTACTS;
                getPictureSelector(this)
                        .forResult(REQUEST_CODE_MULTIPLE_PICTURE);
                break;
            case 6:
                share(SocialShareUtils.ShareType.DEFAULT,VIDEO);
                break;
            case 7:
                mShareType = SocialShareUtils.ShareType.WE_CHAT_CONTACTS;
                share(mShareType,VIDEO);
                break;
            case 8:
                share(SocialShareUtils.ShareType.DEFAULT,ANY);
                break;
            case 9:
                share(SocialShareUtils.ShareType.WE_CHAT_CONTACTS,ANY);
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_MULTIPLE_PICTURE:
                    if (data != null) {
                        List<LocalMedia> photoDatas = PictureSelector.obtainMultipleResult(data);
                        List<String> photos = new ArrayList<>();
                        for (LocalMedia photoData : photoDatas) {
                            photos.add(photoData.getRealPath());
                        }
                        switch (mShareType) {
                            case DEFAULT:
                                SocialShareUtils.shareMultiplePicture(getActivity(),photos);
                                break;
                            case WE_CHAT_CIRCLE:
                                SocialShareUtils.shareMultiplePictureToWeChatCircle(getActivity(),"单图片分享",photos);
                                break;
                            case WE_CHAT_CONTACTS:
                                SocialShareUtils.shareMultiplePictureToWeChatContacts(getActivity(),"单图片分享",photos);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                case REQUEST_CODE_SHARE_PICTURE:
                    SocialShareUtils.sharePicture(getActivity(),data.getData(),mShareType);
                    break;
                case REQUEST_CODE_SHARE_VIDEO:
                    SocialShareUtils.shareVideo(getActivity(),data.getData(),mShareType);
                    break;
                case REQUEST_CODE_SHARE_FILE:
                    SocialShareUtils.shareFile(getActivity(),data.getData(),mShareType);
                    break;
                default:
                    break;
            }
        }
    }

    private void share(SocialShareUtils.ShareType shareType, @IntentUtils.DocumentType String documentType){
        mShareType = shareType;
        switch (documentType) {
            case IMAGE:
                startActivityForResult(IntentUtils.getDocumentPickerIntent(documentType),REQUEST_CODE_SHARE_PICTURE);
                break;
            case VIDEO:
                startActivityForResult(IntentUtils.getDocumentPickerIntent(documentType),REQUEST_CODE_SHARE_VIDEO);
                break;
            case ANY:
                startActivityForResult(IntentUtils.getDocumentPickerIntent(documentType),REQUEST_CODE_SHARE_FILE);
                break;
            default:
                break;
        }
    }
    //配置图片选择器
    public static PictureSelectionModel getPictureSelector(Fragment fragment){
        return PictureSelector.create(fragment)
                .openGallery(PictureMimeType.ofImage())
                .theme(R.style.picture_QQ_style)
                .maxSelectNum(9)
                .minSelectNum(1)
                .selectionMode(PictureConfig.MULTIPLE)
                .previewImage(true)
                .isCamera(true)
                .enableCrop(false)
                .compress(true)
                .previewEggs(true);
    }
}
